package controller

import (
	"Software/global"
	"Software/model"
	"Software/response"
	"github.com/gin-gonic/gin"
	"time"
)

// @Summary 获取反馈名单
// @Produce  json
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /feedback/findFeedBackList [get]
func FindFeedBackList(ctx *gin.Context) {
	var feedbacks []model.FeedBack
	global.DB.Where("status = ?", false).Find(&feedbacks)

	response.Response(ctx, 200, gin.H{
		"list": feedbacks,
	}, "获取成功")
}

// @Summary 学生向服务端反馈
// @Produce  json
// @Param content query string false "反馈内容" maxlength(100)
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /feedback/addFeedBack [post]
func AddFeedBack(ctx *gin.Context) {
	var req model.AddFeedBackReq
	err := ctx.ShouldBind(&req)
	if err != nil {
		response.Response(ctx, 400, nil, "数据绑定失败")
		return
	}

	global.DB.Create(&model.FeedBack{
		IP:      ctx.ClientIP(),
		Content: req.Content,
		Status:  false,
	})

	response.Response(ctx, 200, nil, "反馈成功")
}

// @Summary 管理员处理反馈
// @Produce  json
// @Param feedbackID query string false "反馈信息的ID" maxlength(100)
// @Param status query string false "反馈信息的状态" maxlength(100)
// @Success 200 {object} model.Response "成功"
// @Failure 400 {object} model.Response "请求错误"
// @Router /feedback/auditFeedBack [post]
func AuditFeedBack(ctx *gin.Context) {
	var req model.AuditFeedBackReq
	err := ctx.ShouldBind(&req)
	if err != nil {
		response.Response(ctx, 400, nil, "数据绑定失败")
		return
	}

	global.DB.Where("id = ?", req.FeedBackID).Updates(&model.FeedBack{
		AuditorName: ctx.GetString("auditor_name"),
		AuditTime:   time.Now(),
		Status:      req.Status,
	})

	response.Response(ctx, 200, nil, "审核成功")
}
